import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Scarico extends JFrame {

	private static final long serialVersionUID = 1L;

	private JFrame mw;
	private JLabel labelInfo;
	private JButton buttonInserisci, buttonChiudi;
	private JTextField textCodice;
	private JComboBox comboTaglia;
	private Prodotto prodotto;
	
	public Scarico () { 
		this.setTitle("Scarico Prodotto");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new Closer());
		
		/*
		 * GridBagLayout generation
		 */
		JPanel panel1 = new JPanel();
		panel1.setLayout(new GridBagLayout());
		
		
		/*
		 * Default Constraints
		 */
		GridBagConstraints gc = new GridBagConstraints();
		gc.gridwidth = 1; 
		gc.gridheight = 1;
		gc.insets = new Insets(5,5,5,5); //External Padding
		gc.anchor = GridBagConstraints.EAST;
		gc.fill = GridBagConstraints.NONE;
		
		/*
		 * First column
		 */
		gc.gridx = 0;
		gc.gridy = 0;		
		panel1.add(new JLabel("Codice Prodotto:"), gc);
		
		gc.gridy = 1; 
		panel1.add(new JLabel("Taglia Prodotto:"), gc); 
		
		//labelInfo go from one side to the other side of the Panel
		gc.gridy = 2;
		gc.gridwidth = 3;
		gc.anchor = GridBagConstraints.CENTER;
		labelInfo = new JLabel("Scarico pronto...");
		labelInfo.setHorizontalTextPosition(JLabel.CENTER);
		panel1.add(labelInfo, gc);
		
		/*
		 * Buttons are placed in a seprate Box
		 */
		
		Selector ca = new Selector();
		
		gc.anchor=GridBagConstraints.CENTER;
		gc.gridy = 3;
		gc.gridwidth = 3;
		Box buttonBox = Box.createHorizontalBox();
		buttonInserisci = new JButton ("Scarica Prodotto");
		buttonInserisci.addActionListener(ca);
		buttonInserisci.addKeyListener(ca);
		buttonChiudi = new JButton("Chiudi");
		buttonChiudi.addActionListener(ca);
		buttonBox.add(buttonInserisci);
		buttonBox.add(Box.createHorizontalStrut(20));
		buttonBox.add(buttonChiudi);
		
		panel1.add(buttonBox, gc);
		
		/*
		 * Second column
		 * JTextFields use 2 grids horizontally
		 */
		gc.gridx = 1;
		gc.gridwidth = 2;
		gc.anchor = GridBagConstraints.WEST;
		
		gc.gridy = 0;
		textCodice = new JTextField(30);
		textCodice.addActionListener(ca);
		textCodice.addFocusListener(ca);
		panel1.add(textCodice, gc);
		
		gc.gridy =1;
		comboTaglia = new JComboBox();
		comboTaglia.setEditable(false);
		comboTaglia.addActionListener(ca);
		comboTaglia.addKeyListener(ca);
		panel1.add(comboTaglia, gc);
		
		/*
		 * Add the panel using GridBagLayout to the JFrame
		 * and finalization
		 */
		this.add(panel1);

		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setResizable(false);
  
		prodotto = new Prodotto();
		
	}
	
	public Scarico (JFrame mw) {
		this();
		this.mw = mw;
		mw.setVisible(false);	
	}
	
	private class Selector implements ActionListener, FocusListener, KeyListener {
		
		private void cleanMask() { 
			comboTaglia.removeAllItems();		
			textCodice.setText("");
			textCodice.requestFocus();
			Scarico.this.pack();
		}

		private void textCodiceAction () {
			if (textCodice.getText().length()>0) {
				prodotto.setCodice(textCodice.getText());
				if(prodotto.getNome()!=null) { 
					if (prodotto.getTaglia()!=null) { 
						comboTaglia.addItem(prodotto.getTaglia());
						buttonInserisci.doClick();
					} else {
						comboTaglia.removeAllItems();
						popolaTaglie(prodotto.getTaglie());
						comboTaglia.requestFocus();
					}
				} else { 
					labelInfo.setText("Prodotto non presente in magazzino.");
					cleanMask();
				}
			}
		}
		
		private void popolaTaglie(ArrayList<String> taglie) {
			if (taglie != null) {
				for (String s : taglie) {
					comboTaglia.addItem(s);
				}
			} 
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource() == buttonChiudi) {
				mw.setVisible(true);
				Scarico.this.dispose();
			}
			if(e.getSource()==textCodice) { 
				textCodiceAction();
				
			}
			if (e.getSource() == comboTaglia) {
				prodotto.setTaglia((String)comboTaglia.getSelectedItem());
				prodotto.setQuantita("-1");
			}
			if(e.getSource()==buttonInserisci) { 
				if (prodotto.salvaProdotto()) {
					labelInfo.setText("<HTML><B>Aggiornamento avvenuto con successo!</B>" + 
							"<BR>Codice: " + prodotto.getCodice() + " " + 
							"<BR>Nome: " + prodotto.getNome() + " (" + prodotto.getTaglia() + ")<BR>" + 
							"Quantita rimanente: " + prodotto.getQuantita() + " pezzi" +  
							"<BR>Prezzo di vendita: " + prodotto.getPrezzo() + "</HTML>");
					prodotto = new Prodotto();
					cleanMask();
				} else { 
					labelInfo.setText("<HTML><B>Prodotto esaurito!</B>" + 
							"<BR>Codice: " + prodotto.getCodice() + " " + 
							"<BR>Nome: " + prodotto.getNome() + " (" + prodotto.getTaglia() + ")<BR>" + 
							"Quantita in magazzino: 0 pezzi" +  
							"<BR>Prezzo di vendita: " + prodotto.getPrezzo() + "</HTML>");
					prodotto = new Prodotto();
					cleanMask();
				}
			}
		}

		@Override
		public void focusGained(FocusEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void focusLost(FocusEvent e) {
			if(e.getSource() == textCodice) {
				textCodiceAction();
			}
		}

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getSource()==comboTaglia) { 
				if (e.getKeyChar() == '\n') { 
					buttonInserisci.requestFocus();
				}
			}
			if (e.getSource()==buttonInserisci) {
				if (e.getKeyChar()=='\n') { 
					buttonInserisci.doClick();
				}
			}
			
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
	}
		
	private class Closer implements WindowListener {

		@Override
		public void windowOpened(WindowEvent e) {
			
		}

		@Override
		public void windowClosing(WindowEvent e) {
			mw.setVisible(true);
			Scarico.this.dispose();
		}

		@Override
		public void windowClosed(WindowEvent e) {
		}

		@Override
		public void windowIconified(WindowEvent e) {
			Scarico.this.pack();
		}

		@Override
		public void windowDeiconified(WindowEvent e) {
			Scarico.this.pack();
		}

		@Override
		public void windowActivated(WindowEvent e) {
			
		}

		@Override
		public void windowDeactivated(WindowEvent e) {
			
		}
		
	}
}

